#include "Shape.h"

Shape::Shape():m_dirX(0), m_dirY(0) ,m_sumDirX (0), m_sumDirY(0) , m_printWithSplit(0),
						m_timesDirXChanged(0), m_timesDirYChanged(0){};

bool Shape::legalDirXDirY ( double dirX, double dirY) const
{
	if (abs(dirX) > 1 || abs(dirY) > 1)
	{
		cerr << "Error: a direction must be between -1 to 1 only" << endl;
		return false;
	}

	return true;
}

void Shape::set(Point p1, double dirX, double dirY)
{	
	m_point = p1;
	m_dirX = dirX;
	m_dirY = dirY;
	m_printWithSplit = this->isInBorders();
}

bool Shape::setDirection(double dirX, double dirY)
{
	if (legalDirXDirY(dirX, dirY))
	{
		m_dirX = dirX;
		m_dirY = dirY;
		return true;
	}

	return false;
}

void Shape::updatePrintWithSplit ()
{
	m_printWithSplit = isInBorders();
}

void Shape::initSum()
{
	this->m_sumDirX=0;
	this->m_sumDirY=0;
}

void Shape::saveOriginalDir()
{
	m_originalDirX = m_dirX;
	m_originalDirY = m_dirY;
}

void Shape::restoreOriginalDir()
{
	m_dirX = m_originalDirX;
	m_dirY = m_originalDirY;
}

bool Shape::writeToText (ofstream &file)
{
	//write typeID
	writeTypeId (file);
	//write shape's datamembers
	file << this->getDirX() << " ";
	file << this->getDirY() << " ";
	file << this->getXPoint() << " ";
	file << this->getYPoint() << " ";
	file << this->getPrintWithSplit() << " ";
	//
	return false;

}

void Shape::writeTypeId (ofstream &file)
{


		// Get the typeid of yourself
	// (which is one of the deriveds).
	const char * type = typeid(*this).name();

	// We are worried that the compiler we
	// use writes the word 'class' before the
	// class, so we want to deal with it properly
	const char * blankPos = strchr(type , ' ');
	if(blankPos!=0)
		type = blankPos + 1;

	// Now save it to file...
	// (using the great MyString class)
	file << type << " " ;
	

}